package org.point.service.impl;

import java.io.Serializable;import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.point.common.service.impl.BaseServiceImpl;
import org.point.common.util.DoPage;
import org.point.dao.ConvertIndentDao;
import org.point.dao.ConvertOrderStatusDao;
import org.point.dao.DeliveryInfoDao;
import org.point.dao.ProductDao;
import org.point.dao.UsersDao;
import org.point.dao.impl.ProductDaoImpl;
import org.point.domain.ConvertIndent;
import org.point.domain.ConvertOrderStatus;
import org.point.service.ConvertIndentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @ClassName: ConvertRuleDetailServiceImpl  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午4:02:01  
 *
 */
@Service("convertIndentService")
public class ConvertIndentServiceImpl extends BaseServiceImpl<ConvertIndent, Serializable> implements ConvertIndentService {
	
	@Autowired
	private ConvertIndentDao dao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private UsersDao usersDao;
	
	@Autowired
	private DeliveryInfoDao deliveryInfoDao;
	
	@Autowired
	private ConvertOrderStatusDao cosDao;

	public DoPage<ConvertIndent> searchProOrderForPage(
			DoPage<ConvertIndent> page) {
		List<ConvertIndent> records = dao.findProOrder(page);
		Integer totalRecords = dao.countProOrder(page);
		page.setList(records);
		page.setTotalRecords(totalRecords);
		return page;
	}

	public Integer insertProOrder(ConvertIndent record, String ciCode) {
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		if(record.getCovertId()!=null&&!"".equals(record.getCovertId())){
			map.put("covertId", record.getCovertId());
			Integer del  = dao.delProOrder(map);
			record.setDelFlag(new Byte("0"));
			Integer insert  = dao.insertProOder(record);
			if(insert+del>1){
				result = 1;
		    }
		}else{
			record.setCovertId(ciCode);
			record.setDelFlag(new Byte("0"));
			result = dao.insertProOder(record);
			ConvertOrderStatus cos = new ConvertOrderStatus();
			cos.setOrderId(record.getCovertId());
			cos.setOrderStatus(new Byte("1"));
			cosDao.insertCOS(cos);
 		}
		return result;
	}

	public Hashtable<Object,Object> getProOrderByID(Map<String, Object> map) {
		Hashtable<Object,Object> record = dao.selectProOrderBYid(map);
		return record;
	}

	public Integer deleteProOrders(Map<String, Object> map) {
		Integer result = dao.delProOrderByids(map);
		return result;
	}

	public Integer verifyOrder(Map<String, Object> map) {
		Integer result = 0;
		List<String> orderIds = dao.selectOrderIDs(map); 
		Map<String, Object> orderMap = new HashMap<String, Object>();
		orderMap.put("orderIds", orderIds);
		orderMap.put("orderStatus",new Byte("2"));
		Integer isVerifys = cosDao.verifyOrder(orderMap);
		if(isVerifys>0){
			result=1;
		}
		return result;
	}

	public List<Hashtable<Object, Object>> getProducts() {
		List<Hashtable<Object, Object>> record = productDao.selectAllProduct();
		return record;
	}

	public List<Hashtable<Object, Object>> getUsers(String memberid) {
		List<Hashtable<Object, Object>> record = usersDao.selectAllUsers();
		return record;
	}

	public String getUserIdByiDey(String devId) {
		String record = deliveryInfoDao.selectUserIdByDev_id(devId);
		return record;
	}

	public Hashtable<Object, Object> getDevInfo(Map<String, Object> map) {
		Hashtable<Object, Object> record = deliveryInfoDao.getDevInfo(map);
		return record;
	}

	public Integer cancelOrder(Map<String, Object> map) {
		Integer result = cosDao.cancelOrder(map);
		return result;
	}

	public String getCovertId(Map<String, Object> map) {
		String record = dao.searchCovertID(map);
		return record;
	}
	
}
